package com.codes.controller;

import com.codes.core.common.result.Result;
import com.codes.service.MessageSV;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Date 2020/6/10 17:39
 * @Created by peanut
 */
@Slf4j
@Api(tags = "消息接口")
@RestController
@RequestMapping("/message")
public class MessageCtr {

    @Autowired
    private MessageSV messageSV;

    @ApiOperation("page")
    @PostMapping("/pagewx")
    public Result messagePage(@RequestBody Map<String,Object> map){

        Integer page =  MapUtils.getInteger(map,"page");
        Integer limit = MapUtils.getInteger(map,"limit");
        PageHelper.startPage(page, limit);
        List<Map<String, Object>> users = messageSV.getMessage(map);
        PageInfo<Map<String, Object>> info = new PageInfo<>(users);
        return Result.pages(info.getList(), (int) info.getTotal());
    }

    @ApiOperation("分页")
    @GetMapping("/page")
    public Result getPage(@RequestParam(defaultValue = "1", required = true) int page,
                          @RequestParam(defaultValue = "10", required = true) Integer limit,
                          @RequestParam Map<String, Object> map) {
        PageHelper.startPage(page, limit);
        List<Map<String, Object>> users = messageSV.getMessage(map);
        PageInfo<Map<String, Object>> info = new PageInfo<>(users);
        return Result.pages(info.getList(), (int) info.getTotal());
    }

    @ApiOperation("分页")
    @GetMapping("/count")
    public Result getCount(
            //@RequestBody Map<String, Object> map1,
                           @RequestParam Map<String, Object> map) {
         Integer count  = messageSV.getCount(map);
         return Result.success(count);
    }


    @ApiOperation("消息改为已读")
    @PostMapping("/reading")
    public Result reading(@RequestBody Map<String,Object> map){

        Integer messageId = MapUtils.getInteger(map,"messageId");
        if( null != messageId){
            int re =  messageSV.updateByPrimaryKeySelective(map);
            if(re > 0 ){
                return Result.success("消息已读");
            }
        }
        return Result.failed("消息修改失败");
    }



}